<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="供应商名称" prop="supplierName">
      <el-input v-model="dataForm.supplierName" placeholder="供应商名称"></el-input>
    </el-form-item>
    <el-form-item label="联系人" prop="linkMan">
      <el-input v-model="dataForm.linkMan" placeholder="联系人"></el-input>
    </el-form-item>
    <el-form-item label="联系电话" prop="linkTel">
      <el-input v-model="dataForm.linkTel" placeholder="联系电话"></el-input>
    </el-form-item>
    <el-form-item label="欠款金额" prop="amountOwed">
      <el-input v-model="dataForm.amountOwed" placeholder="欠款金额"></el-input>
    </el-form-item>
    <el-form-item label="地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="地址"></el-input>
    </el-form-item>
    <el-form-item label="备注信息" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注信息"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :disabled="disabledSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
				disabledSubmit:false,
        dataForm: {
          id: 0,
          supplierName: '',
          sellerId: '',
          linkMan: '',
          linkTel: '',
          amountOwed: '',
          address: '',
          status: '',
          remark: ''
        },
        dataRule: {
          supplierName: [
            { required: true, message: '供应商名称不能为空', trigger: 'blur' }
          ],
          linkMan: [
            { required: true, message: '联系人不能为空', trigger: 'blur' }
          ],
          linkTel: [
            { required: true, message: '联系电话不能为空', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/store/tstoresupplier/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.supplierName = data.tStoreSupplier.supplierName
                this.dataForm.sellerId = data.tStoreSupplier.sellerId
                this.dataForm.linkMan = data.tStoreSupplier.linkMan
                this.dataForm.linkTel = data.tStoreSupplier.linkTel
                this.dataForm.amountOwed = data.tStoreSupplier.amountOwed
                this.dataForm.address = data.tStoreSupplier.address
                this.dataForm.status = data.tStoreSupplier.status
                this.dataForm.remark = data.tStoreSupplier.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
				this.disabledSubmit = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/store/tstoresupplier/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'supplierName': this.dataForm.supplierName,
                'sellerId': this.dataForm.sellerId,
                'linkMan': this.dataForm.linkMan,
                'linkTel': this.dataForm.linkTel,
                'amountOwed': this.dataForm.amountOwed,
                'address': this.dataForm.address,
                'status': this.dataForm.status,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
										this.disabledSubmit = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.disabledSubmit = false
                this.$message.error(data.msg)
              }
            })
          }
          else {
            this.disabledSubmit = false
          }
        })
      }
    }
  }
</script>
